package com.wu.springboot_study.config;

import com.wu.springboot_study.entity.User;
import com.wu.springboot_study.service.IUserService;
import lombok.AllArgsConstructor;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author benjamin_5
 * @Description
 * @date 2023/1/9
 */
@Component
@AllArgsConstructor
public class BloomFilterInitConfig {

    private final Logger log = LoggerFactory.getLogger(BloomFilterInitConfig.class);
    private final IUserService userService;
    private final RedissonClient redissonClient;

    @PostConstruct
    public void init(){
        log.info("BloomFilter init start");
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("userBloom");
        // 参数1：预估的key的总数 参数2：期望容错率
        bloomFilter.tryInit(100000L,0.03);
        List<User> list = userService.list();
        for (User user : list) {
            bloomFilter.add(user.getId());
        }
        log.info("BloomFilter init end");
    }
}
